package at.catalysts.imgClassifier.core;

import java.util.ArrayList;
import java.util.List;

import at.catalysts.imgClassifier.dataFinders.ImgReader;
import at.catalysts.imgClassifier.dataFinders.TextReader;
import at.catalysts.imgClassifier.db.FsDatabase;
import at.catalysts.imgClassifier.interpreters.Interpreter;
import at.catalysts.imgClassifier.transformer.HtmlImgExtractor;
import at.jaki.ubb.crawling.model.config.CrawlingEngineConfig;
import at.jaki.ubb.crawling.model.config.DataFinderConfig;
import at.jaki.ubb.crawling.model.config.DataFinderManagerConfig;
import at.jaki.ubb.crawling.model.config.DataTransformerConfig;
import at.jaki.ubb.crawling.model.config.DataTransformerManagerConfig;
import at.jaki.ubb.crawling.stlib.dataFinder.SimpleHttpClient;

public class ConfigGenerator {

	public static CrawlingEngineConfig createMainConfig() {
		CrawlingEngineConfig cec = new CrawlingEngineConfig();

		DataFinderManagerConfig dataFinderManagerConfig = createDataFindersConfig();
		cec.setDataFinderManagerConfig(dataFinderManagerConfig);

		DataTransformerManagerConfig dataTransformerManagerConfig = getDataTransformerManagerConfig();
		cec.setDataTransformerManagerConfig(dataTransformerManagerConfig);

		cec.setDbFacadeClass(FsDatabase.class.getCanonicalName());
		cec.setDataInterpreterClass(Interpreter.class.getCanonicalName());
		return cec;
	}

	private static DataFinderManagerConfig createDataFindersConfig() {
		DataFinderManagerConfig dfmc = new DataFinderManagerConfig();
		List<DataFinderConfig> dataFinders = new ArrayList<DataFinderConfig>();
		dataFinders.add(getFromTextFile());
		dataFinders.add(getPageContent());
		dataFinders.add(getImagesFromUrl());
		dfmc.setDataFinders(dataFinders);
		return dfmc;
	}

	private static DataTransformerManagerConfig getDataTransformerManagerConfig() {
		DataTransformerManagerConfig c = new DataTransformerManagerConfig();
		List<DataTransformerConfig> dataTransformers = new ArrayList<DataTransformerConfig>(1);
		dataTransformers.add(getHttpImgExtrector());
		c.setDataTransformers(dataTransformers);
		return c;
	}

	private static DataTransformerConfig getHttpImgExtrector() {
		DataTransformerConfig dtc = new DataTransformerConfig();
		dtc.setClazzName(HtmlImgExtractor.class.getCanonicalName());
		dtc.setLogName("http img extractor");
		return dtc;
	}

	private static DataFinderConfig getFromTextFile() {
		DataFinderConfig dfc = new DataFinderConfig();
		dfc.setClazzName(TextReader.class.getCanonicalName());
		dfc.setLogName("text file reader");
		return dfc;
	}
	
	private static DataFinderConfig getImagesFromUrl() {
		DataFinderConfig dfc = new DataFinderConfig();
		dfc.setClazzName(ImgReader.class.getCanonicalName());
		dfc.setLogName("img downloader");
		return dfc;
	}
	
	private static DataFinderConfig getPageContent() {
		DataFinderConfig dfc = new DataFinderConfig();
		dfc.setClazzName(SimpleHttpClient.class.getCanonicalName());
		dfc.setLogName("simple http client");
		return dfc;
	}

}
